﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _TaperedReamer4
    {
        public static void TaperedReamer4(Dictionary<string, Object> data)  //锥度铰刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            string path = Convert.ToString(data["path"]);
            double soal = Convert.ToDouble(data["oal"]);
            //double soal = 170;
            double scale = 1; //放大系数
            if (scale == 0 && soal < 67)
            {
                scale = 4.0;
            }
            else if (scale == 0 && soal < 90)
            {
                scale = 3.0;
            }
            else if (scale == 0 && soal < 134)
            {
                scale = 2.0;
            }
            else if (scale == 0 && soal < 178)
            {
                scale = 1.5;
            }
            else if (scale == 0 && soal < 268)
            {
                scale = 1.0;
            }
            else if (scale == 0 && soal < 357)
            {
                scale = 0.75;
            }
            else if (scale == 0)
            {
                scale = 0.5;
            }
            double pi = Math.PI;
            /*double dcn = 14;
            double oal = 170;
            double l = 70;
            double sig = 5 * pi / 180;
            double ldc = 18;
            double fha = 89.99;
            double czc = 2;
            double db = 30;
            double ds = 25;
            double tg = 0.05;*/
            double dcn = Convert.ToDouble(data["dcn"]) * scale;
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double sig = Convert.ToDouble(data["sig"]) * pi / 180;
            double ldc = Convert.ToDouble(data["ldc"]) * scale;
            double fha = Convert.ToDouble(data["fha"]);//89.99,75
            double czc = Convert.ToDouble(data["czc"]);
            double db = Convert.ToDouble(data["db"]) * scale;
            double ds = Convert.ToDouble(data["ds"]) * scale;
            double tg = Convert.ToDouble(data["tg"]);
            double dtan = dcn - 2 * ldc * Math.Tan(sig / 2);
            double dtax = dtan + 2 * l * Math.Tan(sig / 2);
            double dn = dtax;


            if (czc == 0)
            {
                db = 9.045 * scale;
                ds = 6.4 * scale;
                tg = 0.05205;
            }
            if (czc == 1)
            {
                db = 12.065 * scale;
                ds = 9.4 * scale;
                tg = 0.04988;
            }
            if (czc == 2)
            {
                db = 17.78 * scale;
                ds = 14.6 * scale;
                tg = 0.04995;
            }
            if (czc == 3)
            {
                db = 23.825 * scale;
                ds = 19.8 * scale;
                tg = 0.05020;
            }
            if (czc == 4)
            {
                db = 31.267 * scale;
                ds = 25.9 * scale;
                tg = 0.05194;
            }
            if (czc == 5)
            {
                db = 44.399 * scale;
                ds = 37.6 * scale;
                tg = 0.05263;
            }
            if (czc == 6)
            {
                db = 63.348 * scale;
                ds = 53.9 * scale;
                tg = 0.05214;
            }

            double ls = (db - ds) / tg;
            //坐标
            Vector2 v1 = new Vector2(ds / 2 - 0.5, 0);
            Vector2 v2 = new Vector2(ds / 2 + Math.Sqrt(0.25 / (1 + tg * tg / 4)) * tg / 2, -Math.Sqrt(0.25 / (1 + tg * tg / 4)));
            Vector2 v3 = new Vector2(db / 2, -ls);
            Vector2 v4 = new Vector2(dn / 2, v3.Y - v3.X + dn / 2);
            if (czc == 2 || czc == 1) 
            {
                v4 = new Vector2(dn / 2, v3.Y + v3.X - dn / 2);
            }
            Vector2 v6 = new Vector2(dtax / 2, -oal + l);
            Vector2 v7 = new Vector2(dcn / 2, -oal + ldc);
            Vector2 v8 = new Vector2(dtan / 2, -oal);
            Vector2 v11 = new Vector2(-v8.X, v8.Y);
            Vector2 v12 = new Vector2(-v7.X, v7.Y);
            Vector2 v13 = new Vector2(-v6.X, v6.Y);
            Vector2 v15 = new Vector2(-v4.X, v4.Y);
            Vector2 v16 = new Vector2(-v3.X, v3.Y);
            Vector2 v17 = new Vector2(-v2.X, v2.Y);
            Vector2 v18 = new Vector2(-v1.X, v1.Y);
            Vector2 v19 = new Vector2(0, 2);
            Vector2 v20 = new Vector2(0, -oal - 2);
            //线段
            //主视图
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            LwPolyline Arc3 = new LwPolyline();
            Arc3.Vertexes.Add(new LwPolylineVertex(v3));
            Arc3.Vertexes.Add(new LwPolylineVertex(v4));
            Arc3.Vertexes[0].Bulge = (1 - Math.Cos(pi / 4)) * 2 * Math.Cos(pi / 4);
            fun.PolyFill(dxf, Arc3, 0.3, contour);
            Vector2[] Line4 = { v4, v6 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line6 = { v6, v8 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line8 = { v8, v11 };
            fun.drawBLine(dxf, Line8, 0.3, contour);
            Vector2[] Line12 = { v11, v13 };
            fun.drawBLine(dxf, Line12, 0.3, contour);
            Vector2[] Line14 = { v13, v15 };
            fun.drawBLine(dxf, Line14, 0.3, contour);
            LwPolyline Arc15 = new LwPolyline();
            Arc15.Vertexes.Add(new LwPolylineVertex(v15));
            Arc15.Vertexes.Add(new LwPolylineVertex(v16));
            Arc15.Vertexes[0].Bulge = (1 - Math.Cos(pi / 4)) * 2 * Math.Cos(pi / 4);
            fun.PolyFill(dxf, Arc15, 0.3, contour);
            Vector2[] Line16 = { v16, v17 };
            fun.drawBLine(dxf, Line16, 0.3, contour);
            Vector2[] Line17 = { v17, v18 };
            fun.drawBLine(dxf, Line17, 0.3, contour);
            Vector2[] Line18 = { v18, v1 };
            fun.drawBLine(dxf, Line18, 0.3, contour);
            Vector2[] Line19 = { v19, v20 };
            fun.drawPlinetype(dxf, Line19, 0.15, centre, Linetype.Center);
            Vector2[] Line20 = { v17, v2 };
            fun.drawBLine(dxf, Line20, 0.3, contour);
            Vector2[] Line21 = { v16, v3 };
            fun.drawBLine(dxf, Line21, 0.3, contour);
            Vector2[] Line22 = { v15, v4 };
            fun.drawBLine(dxf, Line22, 0.3, contour);
            Vector2[] Line24 = { v13, v6 };
            fun.drawBLine(dxf, Line24, 0.3, contour);
            Line Line26 = new Line(v11, v13);
            Line Line27 = new Line(v8, v6);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize =3.5;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 3.5;
            double offset = 10;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v6, v8), offset * 2 + v3.X / 2 - v8.X / 2, 270, dimensionStyle);
            dim1.UserText = "L = " + Math.Round(l / scale, 2);
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v12, v7), -offset * 2 - ldc, 0, dimensionStyle);
            dim2.UserText = "DCN = " + Math.Round(dcn / scale, 2);
            dxf.AddEntity(dim2);
            //尺寸3
            LinearDimension dim3 = new LinearDimension(new Line(v7, v8), offset + v3.X - (v7.X + v8.X) / 2, 270, dimensionStyle);
            dim3.UserText = "LDC = " + Math.Round(ldc / scale, 2);
            dxf.AddEntity(dim3);
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v8, v1), offset * 3 + v2.X - (v1.X + v8.X) / 2, 270, dimensionStyle);
            dim5.UserText = "OAL = " + Math.Round(oal / scale, 2);
            dxf.AddEntity(dim5);
            //尺寸7
            Angular2LineDimension angdim7 = new Angular2LineDimension(Line26, Line27, offset*2 + l + dtan / 2 / Math.Tan(sig / 2), dimensionStyle);
            angdim7.UserText = "SIG = " + Math.Round(sig / (pi / 180), 2) + "°";
            dxf.AddEntity(angdim7);
            //尺寸9
            LinearDimension dim9 = new LinearDimension(new Line(v13, v6), offset, 0, dimensionStyle);
            dim9.UserText = "DTAX = " + Math.Round(dtax / scale, 2);
            dxf.AddEntity(dim9);
            //尺寸10
            LinearDimension dim10 = new LinearDimension(new Line(v11, v8), -offset, 0, dimensionStyle);
            dim10.UserText = "DTAN = " + Math.Round(dtan / scale, 2);
            dxf.AddEntity(dim10);
            //填充螺旋槽
            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v6));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v8));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
            }
            var x = l / 5.00 * Math.Tan(fha * pi / 180);
            if (x < dcn / 2.00)
            {
                if (fha == 75)
                {
                    fun.hatchsuboflinew(dxf, htype2, hnumst2, -fha, l / 2.00 * 2 * Math.Sin(fha * pi / 180), hline2, contour, 0.3);
                }
                if (fha == 89.99)
                {
                    fun.hatchsuboflinew(dxf, htype2, hnumst2, -fha, l / 2.00 * Math.Sin(fha * pi / 180), hline2, contour, 0.3);
                }
            }
            else
            {
                if (fha == 75)
                {
                    fun.hatchsuboflinew(dxf, htype2, hnumst2, -fha, l / 5.00 * 2 * Math.Sin(fha * pi / 180), hline2, contour, 0.3);
                }
                if (fha == 89.99)
                {
                    fun.hatchsuboflinew(dxf, htype2, hnumst2, -fha, l / 5.00 * Math.Sin(fha * pi / 180), hline2, contour, 0.3);
                }
            }


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}